package edu.isquen.core.form.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import edu.isquen.core.base.action.ParamsTable;
import edu.isquen.core.base.model.ValueObject;
import edu.isquen.core.document.model.Document;
import edu.isquen.core.document.model.Item;
import edu.isquen.core.form.model.element.FormElement;
import edu.isquen.core.form.model.element.FormField;
import edu.isquen.core.form.model.element.FormTextPart;
import edu.isquen.core.user.model.User;
import edu.isquen.util.CommUtil;

public class Form extends ValueObject implements Serializable{

	private static final long serialVersionUID = -881824178873541671L;
	
	private String id;
	
	private String name;
	
	private String description;
	
	private String templatecontext;
	
	private User lastModifyUser;
	
	private Date lastModifyDate;
	
	private String dataSourceId;
	
	private Map<String, FormField> _fields;
	
	private Collection<FormTextPart> _textparts;

	private Collection<FormElement> _elements;
	
//	private Map<String, FormField> _activitys;
	
	public Form (){
		_fields = new LinkedHashMap<String, FormField>(20);
		_textparts = new ArrayList<FormTextPart>(20);
		_elements = new ArrayList<FormElement>(40);
	}
	
	public void addFormTextPart(FormTextPart textPart){
		this._textparts.add(textPart);
		this._elements.add(textPart);
	}
	
	public void addFormField(FormField formfield){
		if(formfield!=null){
			if(formfield.getName()==null || formfield.getName().equals("")){
				formfield.setName(formfield.getId()+"");
			}
			formfield.setFormid(this.getId());
			formfield.set_form(this);
			
			this._fields.put(formfield.getId(), formfield);
			this._elements.add(formfield);
		}
	}
	
	public Document createDocument(ParamsTable params, User user) throws Exception {
		Document newDoc = new Document();
		newDoc.setId(CommUtil.getUUID().toString());
		newDoc.setAuthor(user);
		newDoc.setCreateDate(new Date());
		newDoc.setLastModifyUser(user);
		newDoc.setLastModifyDate(new Date());
		newDoc.setFormId(this.id);
		newDoc.setFormName(this.getName());

		addItems(newDoc, params);
		
		return newDoc;
	}
	
	public Document createDocument(Document doc, ParamsTable params, User user) throws Exception{
		if(doc==null){
			return createDocument(params,user);
		}
//		if (!StringUtils.isBlank(params.getParameterAsString("_flowid"))) {
//			doc.setf(params.getParameterAsString("_flowid"));
//		}
		addItems(doc, params);
		return doc;
	}
	
	public void addItems(Document doc, ParamsTable params) {
		Collection<FormField> fields = this.get_fields().values();
		Iterator<FormField> fieldIter = fields.iterator();
		while (fieldIter.hasNext()) {
			FormField field = (FormField) fieldIter.next();
			if (field == null) {
				continue;
			}

			if (!field.getFieldType().equals(Item.VALUE_TYPE_INCLUDE)) {
				Item item = null;
				if (field.getFieldType().equals(Item.VALUE_TYPE_VARCHAR))
					item = field.createItem(doc, params.getParameterAsString(field.getName()));
				else if (field.getFieldType().equals(Item.VALUE_TYPE_NUMBER)) {
					item = field.createItem(doc, params.getParameterAsString(field.getName()));
				} else if (field.getFieldType().equals(Item.VALUE_TYPE_DATE))
					item = field.createItem(doc, params.getParameterAsString(field.getName()));
				else if (field.getFieldType().equals(Item.VALUE_TYPE_TEXT))
					item = field.createItem(doc, params.getParameterAsString(field.getName()));
				else
					item = field.createItem(doc, params.getParameterAsString(field.getName()));
				item.setDocument(doc);
				item.setType(field.getFieldType());
				item.setFormname(this.getName()); // 设置item所属的form name

				String itemName = item.getName();
				Item tmp = doc.findItem(itemName);
				if (tmp != null) {
					item.setId(tmp.getId());
				} else {
					if (item.getId() == null || item.getId().length() <= 0) {
						try {
							item.setId(CommUtil.getUUID().toString());
						} catch (Exception e) {
						}
					}
				}

				doc.addItem(item); 
			}
		}
	}
	
	public String toHtml(Document doc,ParamsTable params,User user){
		StringBuffer html = new StringBuffer();
		doc.setAuthor(user);
		Collection<FormElement> formElements=this.get_elements();
		for(Iterator<FormElement> iter=formElements.iterator();iter.hasNext();){
			FormElement formElement=iter.next();
			if(formElement instanceof FormField){
				html.append(((FormField)formElement).toHTML());
			}else if(formElement instanceof FormTextPart){
				html.append(((FormTextPart)formElement).toHTML());
			}
		}
		return html.toString();
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getTemplatecontext() {
		return templatecontext;
	}

	public void setTemplatecontext(String templatecontext) {
		if(this._fields!=null){
			this._fields.clear();
		}
		if(this._textparts!=null){
			this._textparts.clear();
		}
		if(this._elements!=null){
			this._elements.clear();
		}
		this.templatecontext = templatecontext;
		try {
			TemplateParser.parseTemplate(this, this.templatecontext);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Collection<FormField> getValueStoreFields() {
		return _fields.values();
	}

	public Map<String, FormField> get_fields() {
		return _fields;
	}

	public void set_fields(Map<String, FormField> fields) {
		_fields = fields;
	}

//	public Map<String, FormField> get_activitys() {
//		return _activitys;
//	}
//
//	public void set_activitys(Map<String, FormField> activitys) {
//		_activitys = activitys;
//	}
//	
	public Collection<FormTextPart> get_textparts() {
		return _textparts;
	}

	public void set_textparts(Collection<FormTextPart> textparts) {
		_textparts = textparts;
	}

	public Collection<FormElement> get_elements() {
		return _elements;
	}

	public void set_elements(Collection<FormElement> elements) {
		_elements = elements;
	}
	
	public String getDataSourceId() {
		return dataSourceId;
	}

	public void setDataSourceId(String dataSourceId) {
		this.dataSourceId = dataSourceId;
	}
	
	public Date getLastModifyDate() {
		return lastModifyDate;
	}

	public void setLastModifyDate(Date lastModifyDate) {
		this.lastModifyDate = lastModifyDate;
	}
	
	public User getLastModifyUser() {
		return lastModifyUser;
	}

	public void setLastModifyUser(User lastModifyUser) {
		this.lastModifyUser = lastModifyUser;
	}
}
